c# Jarray tryparse

118

c# Jarray tryparse -

public static T TryParseJson<T>(this string json, string schema) where T : new()
{
    JsonSchema parsedSchema = JsonSchema.Parse(schema);
    JObject jObject = JObject.Parse(json);

    return jObject.IsValid(parsedSchema) ? 
        JsonConvert.DeserializeObject<T>(json) : default(T);
}

// Usage:
var myType = myJsonString.TryParseJson<AwsomeType>(schema);

Comments

Submit
0 Comments